﻿using System;
using System.Configuration;
using System.Web;

namespace Shuba.Web.master
{
    public partial class parameter_add : basePage
    {
        string msg = "";

        private void BindData()
        {
            Shuba.Framework.Encryption Encryption = new Shuba.Framework.Encryption();
            
            Shuba.Business.AppParam appParam = new Shuba.Business.AppParam();
            Shuba.Model.AppParamInfo bi;
            
            bi = appParam.Get();
            hidID.Value = bi.ROW_ID.ToString();
            txtGuFUp.Text = bi.GU_FUP.ToString();
            txtGuCD.Text = bi.GU_CLOSE_DEAL.ToString();
            txtGuFUpFunding.Text = bi.GU_FUP_FUNDING.ToString();
            txtGuCDFunding.Text = bi.GU_CLOSE_DEAL_FUNDING.ToString();
            txtPurg.Text = bi.PURGING_PERIOD.ToString();
            txtPurgWeb.Text = bi.PURGING_PERIOD_WEB.ToString();
            dtProcessDate.Text = bi.PROCESS_DATE.ToString("dd MMMM yyyy");
            txtNPWP.Text = bi.NPWP.ToString();
            txtTimeOut.Text = bi.TIME_OUT.ToString();
            txtRefresh.Text = bi.REFRESH_TIME.ToString();
            rbProcessDate.SelectedValue = bi.IS_AUTO_PROCESS_DATE;
            txtFinalDedup.Text = bi.JOB_FINAL_DEDUP.ToString();
            rbFinalDedup.SelectedValue = bi.IS_NEED_FINAL_DEDUP;

            string CurPass = Encryption.Decrypt(bi.DB_PASS_NEW);

            txtPass.Attributes.Add("value",CurPass);
            hidOldPass.Value = CurPass;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string acl = GetUserAccess("MSTR_PARAM");
                if (!IsUserAdd(acl) && !IsUserEdit(acl))
                {
                    msg = this.GetGlobalResourceObject("Messages", "NoPrivilege").ToString();
                    AlertAndRedirect(msg, ResolveUrl(ConfigurationManager.AppSettings["default_url"]));
                }

                LabelTitle.Text = GetPageTitle("MSTR_PARAM");

                if (Request.QueryString["a"] == "ed")
                {
                    BindData();
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            bool isValid = true;
            Shuba.Business.AppParam param = new Shuba.Business.AppParam();
            Shuba.Model.AppParamInfo bi =new Shuba.Model.AppParamInfo();

            string ActType = "UPDATE";

            if (!RangeValidator1.IsValid || !RangeValidator2.IsValid ||
                !RangeValidator3.IsValid || !RangeValidator4.IsValid ||
                !RangeValidator5.IsValid || !RangeValidator6.IsValid ||
                !RangeValidator7.IsValid || !RangeValidator8.IsValid ||
                !RequiredFieldValidator1.IsValid || !RequiredFieldValidator2.IsValid ||
                !RequiredFieldValidator3.IsValid || !RequiredFieldValidator4.IsValid ||
                !RequiredFieldValidator5.IsValid || !RequiredFieldValidator6.IsValid || 
                !RequiredFieldValidator7.IsValid || !RequiredFieldValidator8.IsValid ||
                !RequiredFieldValidator9.IsValid || !RequiredFieldValidator10.IsValid)
            {
                isValid = false;
                Alert("");
            }

            if (isValid)
            {
                Shuba.Framework.Encryption Encryption = new Shuba.Framework.Encryption();

                bi.ROW_ID = Convert.ToByte(hidID.Value);
                bi.GU_FUP = Convert.ToByte(txtGuFUp.Text.Trim());
                bi.GU_CLOSE_DEAL = Convert.ToByte(txtGuCD.Text.Trim());
                bi.GU_FUP_FUNDING = Convert.ToByte(txtGuFUpFunding.Text.Trim());
                bi.GU_CLOSE_DEAL_FUNDING = Convert.ToByte(txtGuCDFunding.Text.Trim());
                bi.PURGING_PERIOD = Convert.ToByte(txtPurg.Text.Trim());
                bi.PURGING_PERIOD_WEB = Convert.ToByte(txtPurgWeb.Text.Trim());
                bi.WEB_ROOT_ADDRESS = ConfigurationManager.AppSettings["address"];
                bi.DB_PASS_NEW = Encryption.Encrypt(txtPass.Text.Trim());
                bi.DB_PASS_OLD = Encryption.Encrypt(hidOldPass.Value.ToString().Trim());
                bi.PROCESS_DATE = Convert.ToDateTime(dtProcessDate.Text);
                bi.UPDATE_BY = HttpContext.Current.User.Identity.Name;
                bi.UPDATE_DATE = ProcessingDate();
                bi.ACT_TYPE = ActType;
                bi.NPWP = Convert.ToDouble(txtNPWP.Text.Trim());
                bi.TIME_OUT = Convert.ToInt16(txtTimeOut.Text);
                bi.IS_AUTO_PROCESS_DATE = rbProcessDate.SelectedValue == "1" ? "1" : "0";
                bi.REFRESH_TIME = Convert.ToInt16(txtRefresh.Text);
                bi.JOB_FINAL_DEDUP = Convert.ToInt16(txtFinalDedup.Text);
                bi.IS_NEED_FINAL_DEDUP = rbFinalDedup.SelectedValue == "1" ? "1" : "0";

                try
                {
                    param.Save(bi);
                    AddLog("UPDATE", "UPDATE SYSTEM PARAMETER");
                    msg = this.GetGlobalResourceObject("Messages", "SaveAlert").ToString();
                    AlertAndRedirect(msg, "parameter_list.aspx");
                }
                catch (Exception ex)
                {
                    ErrBox.Visible = true;
                    ErrMsg.Text = ex.Message;
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("parameter_list.aspx");
        }

        protected void rbProcessDate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (hidAuto.Value == "true")
            {
                dtProcessDate.Text = DateTime.Today.ToString("dd MMMM yyyy");
                dtProcessDate.Enabled = false;
            }
            else dtProcessDate.Enabled = true;
        }
    }
}